<template>
  <a-table
    v-if="data && data.length"
    ref="table"
    :columns="columns"
    :rowKey="row => row.Id"
    :dataSource="data"
    :loading="loading"
    :pagination="false"
    @change="handleTableChange"
    @rowClick="rowClick"
    :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
    :bordered="true"
    :defaultExpandAllRows="true"
    :defaultExpandedRowKeys="rowKey"
    size="small"
    :scroll="{ y: height }"
  >
  </a-table>
</template>

<script>
const columns = [
  { title: '部门名', dataIndex: 'Text', width: '100%' }
]

export default {
  mounted () {
    this.getDataList()
  },
  data () {
    return {
      data: [],
      height: window.innerHeight - 216,
      pagination: {
        current: 1,
        pageSize: 20,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`
      },
      filters: {},
      sorter: { field: 'Id', order: 'asc' },
      loading: false,
      columns,
      queryParam: {},
      visible: false,
      selectedRowKeys: [],
      rowKey: ['1', '103223']
    }
  },
  methods: {
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []
      this.loading = true
      this.$http
        .post('/Base_Manage/Base_Department/GetTreeDataList', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          const pagination = { ...this.pagination }
          pagination.total = resJson.Total
          this.pagination = pagination
        })
    },
    rowClick (row) {
      this.changeChildFather(row)
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    changeChildFather (rows) {
      this.$emit('listenTochildEvent', rows)
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    hanldleAdd () {
      this.$refs.editForm.openForm()
    },
    handleEdit (id) {
      this.$refs.editForm.openForm(id)
    },
    handleDelete (ids) {
      var thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk () {
          return new Promise((resolve, reject) => {
            thisObj.submitDelete(ids, resolve, reject)
          })
        }
      })
    },
    submitDelete (ids, resolve, reject) {
      this.$http.post('/Base_Manage/Base_Department/DeleteData', ids).then(resJson => {
        resolve()

        if (resJson.Success) {
          this.$message.success('操作成功!')

          this.getDataList()
        } else {
          this.$message.error(resJson.Msg)
        }
      })
    }
  }
}
</script>
